/* Y o u r   D e s c r i p t i o n                       */

/*                            AppBuilder Photon Code Lib */
/*                                         Version 0.83B */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#include <signal.h>

/* Toolkit headers */
#include <Ph.h>
#include <photon/Pg.h>
#include <Pt.h>
#include <Ap.h>

/* Local headers */
#include "abimport.h"
#include "ph_rline.h"
#include "proto.h"
#include "timerFunction.h"
#include <signal.h>
#include <time.h>

extern lsrRunLine runLine;

int init( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	PtArg_t	arg;
	PgColor_t list[MAX_COLORS];
	TRACE *thisTrace;
	int scale;
	int i;

	extern TRACE rlTraces[RL_MAXCHAN + 2];

	list[0] = PgRGB(255, 0, 0);		/* red */
	list[1] = PgRGB(255, 128, 0);	/* orange */
	list[2] = PgRGB(255, 255, 0);	/* yellow */
	list[3] = PgRGB(0, 255, 0);		/* green */
	list[4] = PgRGB(0, 255, 255);	/* cyan */
	list[5] = PgRGB(0, 0, 255);		/* blue */
	list[6] = PgRGB(255, 0, 255);	/* magenta */
	list[7] = PgRGB(255, 0, 128);	/* pink */

	list[8] = PgRGB(254, 85, 85);
	list[9] = PgRGB(254, 170, 85);
	list[10] = PgRGB(254, 254, 85);
	list[11] = PgRGB(85, 254, 85);
	list[12] = PgRGB(85, 254, 254);
	list[13] = PgRGB(85, 85, 254);
	list[14] = PgRGB(254, 85, 254);
	list[15] = PgRGB(254, 85, 170);

	list[16] = PgRGB(254, 128, 128);
	list[17] = PgRGB(254, 191, 128);
	list[18] = PgRGB(254, 254, 128);
	list[19] = PgRGB(128, 254, 128);
	list[20] = PgRGB(128, 254, 254);
	list[21] = PgRGB(128, 128, 254);
	list[22] = PgRGB(254, 128, 254);
	list[23] = PgRGB(254, 128, 190);

	list[24] = PgRGB(254, 190, 190);
	list[25] = PgRGB(254, 222, 190);
	list[26] = PgRGB(254, 254, 190);
	list[27] = PgRGB(190, 254, 190);
	list[28] = PgRGB(190, 254, 254);
	list[29] = PgRGB(190, 190, 254);
	list[30] = PgRGB(254, 190, 254);
	list[31] = PgRGB(254, 190, 222);

	/* actions called before the oscope widget is realized */
	if(apinfo->reason == ABR_PRE_REALIZE) {
		/* eliminate 'unreferenced' warnings */
		link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

		for(i = 0; i < MAX_COLORS; i++) runLine.traceColors[i] = list[i];

		/*
		 * increase the size of the drawing buffer to 16K, i.e., 16 * 1024 = 16384
		 */
		PgSetDrawBufferSize(16384);
	}

	/* actions called after the oscope widget is realized */
	else {
		PhSysInfo_t si;
		if(PtQuerySystemInfo( link_instance, &si) == NULL ||
		   !(si.gfx.valid_fields & Ph_GFX_CAPABILITIES) ||
		   (si.gfx.capabilities	& ( Ph_GCAP_MASKED_BLIT | Ph_GCAP_PALETTE)) !=
		   (Ph_GCAP_MASKED_BLIT | Ph_GCAP_PALETTE)) {

			PtSetArg(&arg, Pt_ARG_FLAGS, Pt_TRUE, Pt_GHOST | Pt_BLOCKED);
		}

		scale = 0;
		runLine.scaleFactor = scale;

		/* set the select colors of the buttons */
		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[0], 0);
		PtSetResources(ABW_chan0Toggle, 1, &arg);

		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[1], 0);
		PtSetResources(ABW_chan1Toggle, 1, &arg);

		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[2], 0);
		PtSetResources(ABW_chan2Toggle, 1, &arg);

		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[3], 0);
		PtSetResources(ABW_chan3Toggle, 1, &arg);

		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[4], 0);
		PtSetResources(ABW_chan4Toggle, 1, &arg);

		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[5], 0);
		PtSetResources(ABW_chan5Toggle, 1, &arg);

		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[6], 0);
		PtSetResources(ABW_chan6Toggle, 1, &arg);

		PtSetArg(&arg, Pt_ARG_SET_COLOR, list[7], 0);
		PtSetResources(ABW_chan7Toggle, 1, &arg);

		/* set the states of the toggle buttons */
		for(i = 0; i < RL_MAXCHAN + 2; i++) {
			thisTrace = &rlTraces[i];
			if(thisTrace->rl_on) {
				PtSetArg(&arg, Pt_ARG_FLAGS, Pt_TRUE, Pt_SET);
				switch(i) {
				case 0:
					PtSetResources(ABW_chan0Toggle, 1, &arg);
					break;
				case 1:
					PtSetResources(ABW_chan1Toggle, 1, &arg);
					break;
				case 2:
					PtSetResources(ABW_chan2Toggle, 1, &arg);
					break;
				case 3:
					PtSetResources(ABW_chan3Toggle, 1, &arg);
					break;
				case 4:
					PtSetResources(ABW_chan4Toggle, 1, &arg);
					break;
				case 5:
					PtSetResources(ABW_chan5Toggle, 1, &arg);
					break;
				case 6:
					PtSetResources(ABW_chan6Toggle, 1, &arg);
					break;
				case 7:
					PtSetResources(ABW_chan7Toggle, 1, &arg);
					break;
				case 8:
					PtSetResources(ABW_rlineToggle, 1, &arg);
					break;
				case 9:
					PtSetResources(ABW_unitsToggle, 1, &arg);
					break;
				}
			}
			else {
				PtSetArg(&arg, Pt_ARG_FLAGS, Pt_FALSE, Pt_SET);
				switch(i) {
				case 0:
					PtSetResources(ABW_chan0Toggle, 1, &arg);
					break;
				case 1:
					PtSetResources(ABW_chan1Toggle, 1, &arg);
					break;
				case 2:
					PtSetResources(ABW_chan2Toggle, 1, &arg);
					break;
				case 3:
					PtSetResources(ABW_chan3Toggle, 1, &arg);
					break;
				case 4:
					PtSetResources(ABW_chan4Toggle, 1, &arg);
					break;
				case 5:
					PtSetResources(ABW_chan5Toggle, 1, &arg);
					break;
				case 6:
					PtSetResources(ABW_chan6Toggle, 1, &arg);
					break;
				case 7:
					PtSetResources(ABW_chan7Toggle, 1, &arg);
					break;
				case 8:
					PtSetResources(ABW_rlineToggle, 1, &arg);
					break;
				case 9:
					PtSetResources(ABW_unitsToggle, 1, &arg);
					break;
				}
			}
		}
		
		/* update the toggle button labels */
		updateLabels();
	}

	return( Pt_CONTINUE );
}
